package com.inno.life.controller.error;


import com.inno.life.common.entity.CommonReturnType;
import com.inno.life.common.enums.ResultCodeEnum;
import com.inno.life.common.error.BusinessException;
import feign.RetryableException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author sean
 */
@Slf4j
@RestControllerAdvice
public class DefaultErrorHandler {
    @ExceptionHandler(BusinessException.class)
    public CommonReturnType<Object> handle(BusinessException e) {
        log.error("业务异常:", e);
        return CommonReturnType.fail(ResultCodeEnum.FAIL.getCode(), e.getMessage());
    }

    @ExceptionHandler(NullPointerException.class)
    public CommonReturnType<Object> handle(NullPointerException e) {
        log.error("NullPointerException:", e);
        return CommonReturnType.fail(ResultCodeEnum.FAIL.getCode(), e.getMessage());
    }
    @ExceptionHandler(IllegalArgumentException.class)
    public CommonReturnType<Object> handle(IllegalArgumentException e) {
        log.error("IllegalArgumentException:", e);
        return CommonReturnType.fail(ResultCodeEnum.FAIL.getCode(), e.getMessage());
    }
    @ExceptionHandler(RetryableException.class)
    public CommonReturnType<Object> handle(RetryableException e) {
        log.error("RetryableException:", e);
        return CommonReturnType.fail(ResultCodeEnum.FAIL.getCode(), "请求超时,请刷新重试");
    }

    @ExceptionHandler(Exception.class)
    public CommonReturnType<Object> handle(Exception e) {
        log.error("系统异常:", e);
        return CommonReturnType.fail(ResultCodeEnum.FAIL.getCode(), StringUtils.isBlank(e.getMessage()) ? "系统繁忙 请稍后重试" : e.getMessage());
    }
}
